package com.spring.server;

import com.alibaba.fastjson.JSON;
import com.spring.utils.Assert;
import com.spring.utils.BeanUtil;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

public class SpringServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.解析请求的url
        String requestUrl = "doGet" + pareRequestURI(req);
        //3.根据 请求的url获取要使用的类
        String classNameByGet = HandlerMapping.getMethod(requestUrl);
        if (Assert.isNotBlank(classNameByGet)){
            excute(req,resp,classNameByGet);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //1.解析请求的url
        String requestUrl = "doPost" + pareRequestURI(req);
        //3.根据 请求的url获取要使用的类
        String classNameByGet = HandlerMapping.getMethod(requestUrl);
        if (Assert.isNotBlank(classNameByGet)){
            excute(req,resp,classNameByGet);
        }
    }

    /**
     * 執行方法分發
     * @param req
     * @param resp
     * @param classNameByGet
     */
    private void excute(HttpServletRequest req, HttpServletResponse resp, String classNameByGet){
        // 切割class 包路径
        String[] classMethod = classNameByGet.split(":");
        Class<?> aClass = BeanUtil.getClass(classMethod[0]);
        if (aClass != null){
            Method method = BeanUtil.getMethod(classMethod[1], aClass);
            if (method != null){
                try {
                    Object invoke = method.invoke(BeanUtil.instanceClass(aClass));
                    if (invoke != null){
                        String json = JSON.toJSONString(invoke);
                        resp.setHeader("content-type", "application/json;charset=UTF-8");
                        resp.getWriter().write(json);
                    }
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (InvocationTargetException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }else {
            throw new RuntimeException("获取class失败！");
        }
    }

    /**
     * 解析url
     * @param request
     * @return
     */
    private String pareRequestURI(HttpServletRequest request){
        String contextPath = request.getContextPath();
        String requestURI = request.getRequestURI();
        if (Assert.isNotBlank(contextPath)){
            String midUrl = requestURI.replace(contextPath, "");
            return midUrl;
        }
        return requestURI;
    }

    /**
     * 重写了Servlet的init方法后一定要记得调用父类的init方法，
     * 否则在service/doGet/doPost方法中使用getServletContext()方法获取ServletContext对象时
     * 就会出现java.lang.NullPointerException异常
     * @param config
     * @throws ServletException
     */
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
    }


}
